﻿using CommonBase;
using Microsoft.Practices.Prism.Regions;
using PersonnelLocationModule.Models;
using SurveillancePro.Base.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;

namespace PersonnelLocationModule.ViewModels
{
    /// <summary>
    /// 当前站点人员信息
    /// </summary>
    public class ViewModel_CurrentSiteEmployees : WorkspaceViewModel
    {
        #region Static Fields

        /// <summary>
        /// 所有人员列表信息【静态】
        /// </summary>
        private static ObservableCollection<EmployeeModel> AllEmployeesList;

        #endregion

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="employeeIDs">区域站点人员 ID 列表</param>
        /// <param name="regionName">区域名称</param>
        /// <param name="siteName">站点名称</param>
        public ViewModel_CurrentSiteEmployees(IRegionManager regionManager,
            Dictionary<string, string> employeeIDs, string regionName, string siteName)
        {
            base.BaseRegionManager = regionManager;
            base.ViewName = SurveillancePro.Base.Common.ViewNames.PersonnelLocationModule_CurrentSiteEmployees;
            base.HeaderTitle = string.IsNullOrWhiteSpace(regionName) || string.IsNullOrWhiteSpace(siteName) ? "站点人员" :
                string.Format("站点人员[{0}-{1}]", regionName, siteName);
            base.HeaderImageSource = string.Format(ConstantString.IMAGE_URL, "Question");
            base.IsShowCloseButton = Visibility.Visible;

            if (AllEmployeesList == null || AllEmployeesList.Count <= 0)
            {
                DataRowCollection rows = EmployeeModel.Load_All(); // 获得所有人员信息
                EmployeeModel[] employees = _loadAll(rows);
                _loadEmployeeList(employees);
            }

            EmployeeList = new ObservableCollection<EmployeeModel>();
            foreach (KeyValuePair<string, string> employeeID in employeeIDs)
            {
                EmployeeModel dummy = new EmployeeModel()
                {
                    EmpID = employeeID.Key,
                };

                int ix = AllEmployeesList.IndexOf(dummy);
                if (ix >= 0)
                {
                    EmployeeModel centainEmployee = AllEmployeesList[ix];
                    centainEmployee.EmpEnterTime = employeeID.Value;

                    EmployeeList.Add(centainEmployee); // 加入到当前人员列表
                }
            }
        }

        #endregion

        #region Base Class Overrides

        protected override void OnDispose()
        {
            _dispose();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 当前人员列表
        /// </summary>
        public ObservableCollection<EmployeeModel> EmployeeList { get; private set; }

        #endregion

        #region Event Handling Methods

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (EmployeeModel employee in e.NewItems)
                    employee.PropertyChanged += this.OnEmployeeModelPopertyChanged;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (EmployeeModel employee in e.OldItems)
                    employee.PropertyChanged -= this.OnEmployeeModelPopertyChanged;
        }

        void OnEmployeeModelPopertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // Blank for now
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 获取所有 Employee Model -- 绑定 Model 中的方法与属性
        /// </summary>
        /// <param name="employeeList"></param>
        /// <returns></returns>
        private EmployeeModel[] _loadAll(DataRowCollection employeeList)
        {
            if (employeeList == null || employeeList.Count <= 0)
            {
                return null;
            }

            EmployeeModel[] _employees = new EmployeeModel[employeeList.Count];
            for (int i = 0; i < employeeList.Count; i++)
                _employees[i] = new EmployeeModel(employeeList[i]);

            return _employees;
        }

        /// <summary>
        /// 获取指定 Employee Model -- 绑定 Model 中的方法与属性
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        private EmployeeModel _loadOne(DataRow employee)
        {
            return new EmployeeModel(employee);
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        /// <param name="employees"></param>
        private void _loadEmployeeList(EmployeeModel[] employees)
        {
            if (employees == null)
            {
                return;
            }

            AllEmployeesList = new ObservableCollection<EmployeeModel>();
            foreach (EmployeeModel employee in employees)
            {
                employee.PropertyChanged += this.OnEmployeeModelPopertyChanged;
                AllEmployeesList.Add(employee);
            }
            AllEmployeesList.CollectionChanged += this.OnCollectionChanged;
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        private void _dispose()
        {
            if (EmployeeList != null)
            {
                foreach (EmployeeModel employee in EmployeeList)
                    employee.Dispose();

                EmployeeList.Clear();
            }

            if (AllEmployeesList != null)
            {
                foreach (EmployeeModel employee in AllEmployeesList)
                    employee.Dispose();

                AllEmployeesList.Clear();
                AllEmployeesList.CollectionChanged -= this.OnCollectionChanged;
            }
        }

        #endregion
    }
}
